package com.biao.mall.member.controller;

import com.biao.common.utils.PageUtils;
import com.biao.common.utils.R;
import com.biao.mall.member.entity.MemberEntity;
import com.biao.mall.member.exception.PhoneExistException;
import com.biao.mall.member.exception.UserNameExistException;
import com.biao.mall.member.feign.CouponFeignService;
import com.biao.mall.member.service.MemberService;
import com.biao.mall.member.vo.MemberLoginVo;
import com.biao.mall.member.vo.MemberRegistVo;
import com.biao.mall.member.vo.SocialUser;
import com.biao.mall.member.vo.SocialUserWB;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;

import static com.biao.common.exception.BizCodeEnume.*;


/**
 * 会员
 *
 * @author biao
 * @email 2845548112@qq.com
 * @date 2022-12-25 15:54:20
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;
    @Autowired
    private CouponFeignService couponFeignService;

    @PostMapping("/oauth2/loginWB")
    R oauth2loginWB(@RequestBody SocialUserWB socialUser) {
        try {
            MemberEntity memberEntity = memberService.socialLoginWB(socialUser);
            return R.ok().put("data", memberEntity);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
            return R.error(GITEE_LOGIN_EXCEPTION.getCode(), GITEE_LOGIN_EXCEPTION.getMsg());
        }
    }
    @PostMapping("/oauth2/login")
    public R oauth2login(@RequestBody SocialUser vo) {

        try {
            MemberEntity memberEntity = memberService.socialLogin(vo);
            return R.ok().put("data", memberEntity);
        } catch (Exception e) {
            return R.error(GITEE_LOGIN_EXCEPTION.getCode(), GITEE_LOGIN_EXCEPTION.getMsg());

        }

    }


    @PostMapping("/login")
    public R login(@RequestBody MemberLoginVo vo) {
        MemberEntity memberEntity = memberService.login(vo);
        if (memberEntity == null) {
            return R.error(LOGINACCT_PASSWORD_INVAILD_EXCEPTION.getCode(), LOGINACCT_PASSWORD_INVAILD_EXCEPTION.getMsg());
        }
        return R.ok().put("data", memberEntity);
    }


    @PostMapping("/regist")
    public R regist(@RequestBody MemberRegistVo vo) {
        try {
            memberService.regist(vo);
        } catch (PhoneExistException e) {
            return R.error(PHONE_EXIST_EXCEPTION.getCode(), PHONE_EXIST_EXCEPTION.getMsg());
        } catch (UserNameExistException e) {
            return R.error(USER_EXIST_EXCEPTION.getCode(), USER_EXIST_EXCEPTION.getMsg());
        }
        return R.ok();
    }


    @GetMapping("/feign")
    public R feignTest() {
        R feign = couponFeignService.feign();
        return feign;
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody MemberEntity member) {
        memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MemberEntity member) {
        memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
